﻿@namespace MudBlazor.Docs.Examples

<MudPaper Class="pa-4">
    <MudChart ChartType="ChartType.HeatMap" ChartSeries="@_series" ChartOptions="@_options"
              ChartLabels="@_xLabels" Width="100%" Height="350px"></MudChart>
</MudPaper>
<MudPaper Class="pa-4 mt-2 d-flex justify-center">
    <MudButton OnClick="AddColor" Disabled="@(_colorCount >= 5)" Variant="Variant.Filled" Color="Color.Primary">Add Color</MudButton>
    <MudButton @onclick="RandomizeData" Variant="Variant.Filled" Class="mx-4">Randomize</MudButton>
    <MudButton OnClick="RemoveColor" Disabled="@(_colorCount <= 1)" Variant="Variant.Filled" Color="Color.Secondary">Remove Color</MudButton>
</MudPaper>

<MudStack Row Justify="Justify.Center">
    @for (var i = 0; i < _colors.Length; i++)
    {
        <MudPaper Class="pa-2 mx-1"
                  Style="@($"background-color: {_colors[i]}; width: 50px; height: 50px;{(i > _colorCount -1 ? string.Empty : "border: 2px solid black;" )}")">
        </MudPaper>
    }
</MudStack>

<MudText Align="Align.Center" Typo="Typo.h6">Number of Colors Selected: @_colorCount</MudText>

@code {
    private int _colorCount = 1;
    private readonly string[] _colors = ["#5AC8FA", "#34C759", "#007AFF", "#FFCC00", "#e03131"];
    private List<ChartSeries<double>> _series = [];
    private ChartOptions _options = new();
    private string[] _xLabels = [];

    protected override void OnInitialized()
    {
        base.OnInitialized();
        BuildOptions();
        RandomizeData();
    }

    private void AddColor()
    {
        _colorCount++;
        BuildOptions();
    }

    private void RemoveColor()
    {
        _colorCount--;
        BuildOptions();
    }

    private void BuildOptions()
    {
        var options = new ChartOptions
        {
            ShowToolTips = false,
            ChartPalette = _colors.Take(_colorCount).ToArray()
        };
        _options = options;
    }

    private void RandomizeData()
    {
        string[] xaxis = ["A", string.Empty, "C",];
        var heatMapSeries = new List<ChartSeries<double>>();
        var dataPoints = xaxis.Length;
        foreach (var x in xaxis)
        {
            var data = new double[dataPoints];
            for (int i = 0; i < dataPoints; i++)
            {
                data[i] = Math.Round(Random.Shared.NextDouble() * 100, 2);
            }
            heatMapSeries.Add(new ChartSeries<double> { Name = x, Data = data });
        }
        _xLabels = xaxis;
        _series = heatMapSeries;
        BuildOptions();
    }
}
